package com.hera.process.config.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hera.base.util.FiltroDTO;
import com.hera.process.config.dao.StageDao;
import com.hera.process.config.dao.StageRollupDao;
import com.hera.process.config.service.StageService;
import com.hera.process.workflow.domain.Stage;
import com.hera.process.workflow.domain.StageRollup;

@Service("stageservice")
public class StageServiceImpl implements StageService {

	static final Logger logger = Logger.getLogger(StageServiceImpl.class);
	
	@Autowired
	private StageDao stagedao;
	@Autowired
	private StageRollupDao stageRollupDao;
	
	@Override
	public Stage findById(Long id) {
		
		return stagedao.findOne(id);
	}

	@Override
	public void update(Stage entity) {
		
		stagedao.save(entity);
	}

	@Override
	public void save(Stage entity) {
		
		stagedao.save(entity);		
		if(entity.getName() == null || entity.getName().trim() == ""){
			entity.setName("STAGE" + entity.getIdStage());
			stagedao.save(entity);
		}
	}

	@Override
	public void delete(Stage entity) {
		//Eliminamos primero las referencias a los stage rollups (de las transiciones)
		FiltroDTO filtroDTO = new FiltroDTO();
		filtroDTO.addParametro("stageFrom.idStage", entity.getIdStage());
		List<StageRollup> listRollups1 = stageRollupDao.obtenerEntidadesPorFiltros(filtroDTO);
		for(int i=0; i<listRollups1.size(); i++){
			listRollups1.get(i).setStageFrom(null);
			listRollups1.get(i).setReferenceDiagram(listRollups1.get(i).getReferenceDiagram().replaceAll("\"" + entity.getIdFigure() + "\"", "null"));
			/*if(listRollups1.get(i).getStageTo() == null){
				stageRollupDao.delete(listRollups1.get(i));
			}else{*/
				stageRollupDao.save(listRollups1.get(i));
			//}
		}
		
		FiltroDTO filtroDTO2 = new FiltroDTO();
		filtroDTO2.addParametro("stageTo.idStage", entity.getIdStage());
		List<StageRollup> listRollups2 = stageRollupDao.obtenerEntidadesPorFiltros(filtroDTO2);
		for(int i=0; i<listRollups2.size(); i++){
			listRollups2.get(i).setStageTo(null);
			listRollups2.get(i).setReferenceDiagram(listRollups2.get(i).getReferenceDiagram().replaceAll("\"" + entity.getIdFigure() + "\"", "null"));
			/*if(listRollups2.get(i).getStageFrom() == null){
				stageRollupDao.delete(listRollups2.get(i));
			}else{*/
				stageRollupDao.save(listRollups2.get(i));
			//}
		}
		
		stagedao.delete(entity);
	}

	@Override
	public List<Stage> findAll() {
		
		return stagedao.findAll();
	}

	@Override
	public List<Stage> findAll(FiltroDTO filtroDTO) {
		
		return stagedao.obtenerEntidadesPorFiltros(filtroDTO);
	}
	
	@Override
	public List<Stage> findStageByProcessId(Long idProcess) {
		
		return stagedao.findStageByProcessId(idProcess);
	}

	@Override
	public List<Stage> findStageByProcessAndIdDiagram(Long idProcess,
			String idDiagram) {
		
		return stagedao.findStageByProcessAndIdDiagram(idProcess, idDiagram);
	}

	@Override
	public List<Stage> findStageByProcessIdandTypeId(Long idProcess, Long idType) {
		
		return stagedao.findStageByProcessIdandTypeId(idProcess, idType);
	}

	
	
}
